package com.jcxy.service.impl;

import com.jcxy.VO.UserVo;
import com.jcxy.mapper.DeptMapper;
import com.jcxy.pojo.Dept;
import com.jcxy.service.DeptService;
import com.jcxy.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public PageBean queryListByPage(Map dto, Integer page, Integer limit) {

        Integer count = deptMapper.queryDeptCount(dto);
        PageBean pageBean = new PageBean(page, limit);
        dto.put("pageIndex",pageBean.getPageIndex());
        dto.put("limit",pageBean.getLimit());
        List<Dept> deptList = deptMapper.queryDept(dto);

        pageBean.setResponseData(count,deptList);
        return pageBean;
    }

    @Override
    @Transactional
    public void insertDept(Map dto) throws Exception {

        Dept dept = new Dept();
        dept.setDept_name(dto.get("dept_name").toString());
        dept.setDept_addr(dto.get("dept_addr").toString());
        dept.setDept_email(dto.get("dept_email").toString());
        dept.setDept_fax(dto.get("dept_fax").toString());
        dept.setDept_tel(dto.get("dept_tel").toString());

        //插入部门基本信息
        deptMapper.insertDept(dept);
        //获取部门所拥有的职位id
        String[] jobIds = dto.get("jobIds").toString().split(",");
        for (String jobId : jobIds){
            dept.setPosition_id(jobId);
            //插入职位-部门关系信息
            deptMapper.insertDeptRelationPosition(dept);
        }
    }

    @Override
    @Transactional
    public int updateDept(Map dto) throws Exception {

        //更新职位基本信息
        int count = deptMapper.updateDept(dto);

        if (count > 1){
            throw new Exception("修改部门信息异常！");
        }else {
            Dept dept = new Dept();
            dept.setDept_id(Integer.valueOf(dto.get("dept_id").toString()));
            //删除部门拥有的职位
            deptMapper.deleteDeptRelationPosition(dept.getDept_id());

            //插入职位与新的部门关系信息
            //获取部门拥有的职位id
            String[] jobIds = dto.get("jobIds").toString().split(",");
            for (String jobId : jobIds){
                dept.setPosition_id(jobId);
                //插入职位-部门关系信息
                deptMapper.insertDeptRelationPosition(dept);
            }
        }
        return count;
    }

    @Override
    public PageBean queryDeptStaffListByPage(Map dto, Integer page, Integer limit) {

        //查询部门员工数
        Integer count = deptMapper.queryDeptStaffCount(dto);
        PageBean pageBean = new PageBean(page, limit);
        dto.put("pageIndex",pageBean.getPageIndex());
        dto.put("limit",pageBean.getLimit());
        List<UserVo> userList = deptMapper.queryDeptStaff(dto);

        pageBean.setResponseData(count,userList);
        return pageBean;
    }

    @Override
    @Transactional
    public int updateDeptManager(Map dto) throws Exception {

        int count = deptMapper.updateDeptManager(dto);

        if (count > 1){
            throw new Exception("修改部门领导失败！");
        }
        return count;
    }

    @Override
    @Transactional
    public boolean updateUserDeptJob(Map dto) throws Exception {

        boolean isManager = false;
        int count = deptMapper.updateUserDeptJob(dto);

        if (count > 1){
            throw new Exception("修改员工的部门、职位失败！");
        }else {
            //判断该员工是否为部门经理，是部门经理的话要把位置让出
            int num = deptMapper.queryDeptCount(dto);
            if (num > 0){
                //是部门经理，移除部门的领导
                dto.remove("user_id");
                dto.put("dept_id",dto.get("cur_dept_id"));
                deptMapper.updateDeptManager(dto);
                isManager = true;
            }
        }
        return isManager;
    }

    @Override
    @Transactional
    public int deleteDept(Map dto) throws Exception {

        //判断部门是否还有部员
        Integer staffCount = deptMapper.queryDeptStaffCount(dto);
        if(staffCount > 0){
            throw new Exception("不能删除有员工的部门！");
        }else {

            int count = deptMapper.deleteDept(Integer.valueOf(dto.get("dept_id").toString()));

            if(count > 1){
                throw new Exception("删除部门异常！");
            }
            return count;
        }
    }
}
